package com.chw.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.chw.common.FanHui;
import com.chw.mapp.ShopMappr;
import com.chw.model.Imgs;
import com.chw.model.Shop;
import com.chw.model.ShopValues;
import com.chw.model.vo.ImgsVo;
import com.chw.model.vo.ShopVo;
import com.chw.model.vo.shopFanHUi;
import com.chw.service.ShopService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
//配置事务
@Transactional
public class ShopServiceImpl implements ShopService {
    @Autowired
    private ShopMappr shopMappr;

    @Override
    public void addShop(Shop shop, String values,String imgs) {
        shopMappr.insertShop(shop);
        //处理json数据，需要导入接送阿里的fastjson包
        JSONArray obj= JSONArray.parseArray(values);
        //声明一个list对象
        List<ShopValues>list=new ArrayList<>();
        //遍历json数组
        for (int i=0;i<obj.size();i++){
            //把json数组赋给 json对象
            JSONObject object= (JSONObject) obj.get(i);
            //声明实体类把json对象转成字符串放入values属性
            ShopValues shopValues=new ShopValues();
            shopValues.setValuess(object.toJSONString());
            //获取主表的id作为pid的值
            shopValues.setPid(shop.getId());
            list.add(shopValues);

        }
        //调用方法新增入库
        shopMappr.addShopValues(list);

        //处理json数据，需要导入接送阿里的fastjson包
        JSONArray objImg= JSONArray.parseArray(imgs);
        //声明一个list对象
        List<Imgs>listImg=new ArrayList<>();
        //遍历json数组
        for (int i=0;i<objImg.size();i++){
            //把json数组赋给 json对象
            JSONObject objectImg= (JSONObject) objImg.get(i);
            //声明实体类把json对象转成字符串放入values属性
           Imgs img =new Imgs();
            img.setImg(objectImg.toJSONString());

            //获取主表的id作为pid的值
            img.setImg(objectImg.getString("url"));
            img.setName(objectImg.getString("name"));
            img.setPid(shop.getId());
            listImg.add(img);

        }
        //调用方法新增入库
        shopMappr.addShopImg(listImg);
    }
    public void upImgs(String imgs,Integer pid,Integer id){
        //处理json数据，需要导入接送阿里的fastjson包
        JSONArray objImg= JSONArray.parseArray(imgs);
        //声明一个list对象
        List<Imgs>listImg=new ArrayList<>();
        //遍历json数组
        for (int i=0;i<objImg.size();i++){
            //把json数组赋给 json对象
            JSONObject objectImg= (JSONObject) objImg.get(i);
            //声明实体类把json对象转成字符串放入values属性
            Imgs img =new Imgs();
            img.setImg(objectImg.toJSONString());

            //获取主表的id作为pid的值
            img.setImg(objectImg.getString("url"));
            img.setName(objectImg.getString("name"));
            listImg.add(img);

        }
        //调用方法新增入库
        shopMappr.upImgs(listImg,pid,id);
    }

    @Override
    public void updateValuesByPid(Integer pid, String values) {
                shopMappr.delScope(pid);
                JSONArray objects=JSONArray.parseArray(values);
                List<ShopValues> shopValues=new ArrayList<>();
        for (int i = 0; i <objects.size() ; i++) {
            JSONObject o= (JSONObject) objects.get(i);
            ShopValues shopValues1=new ShopValues();
            shopValues1.setValuess(o.toJSONString());
            shopValues1.setPid(pid);
            shopValues.add(shopValues1);
        }
        shopMappr.updateValuesByPid(shopValues);
    }

    @Override
    public Object queryShop(ShopVo shop) {
        FanHui fanHui=new FanHui();
        Long count =shopMappr.count(shop);
        List<shopFanHUi> list=shopMappr.queryShop(shop);
        fanHui.setCount(count);
        fanHui.setData(list);
        return fanHui;
    }

    @Override
    public void delShop(Integer id) {
        shopMappr.delShop(id);
    }

    @Override
    public void upShop(Shop shop) {
        shopMappr.upShop(shop);
    }

    @Override
    public  List<ImgsVo> queryImgs(Integer id) {
        List<ImgsVo>imgs= shopMappr.queryImgs(id);
        return imgs;
    }
}
